package kr.pe.asuraho;

import static kr.pe.asuraho.StaticMethods.crontabCode;
import static kr.pe.asuraho.StaticMethods.nvl;

import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class SchedulerListener implements ServletContextListener {
	

	@Override
	public void contextDestroyed(ServletContextEvent context) {
		try {
			Scheduler sd = StdSchedulerFactory.getDefaultScheduler();
			
			sd.deleteJob(new JobKey("HibogoJob"));
			JobDetail job = JobBuilder.newJob(SchedulerJob.class).withIdentity("HibogoJob").withDescription("스따또~").build();
			
			
			
			StdSchedulerFactory.getDefaultScheduler().shutdown();
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		DBConn.close();
	}

	@Override
	public void contextInitialized(ServletContextEvent context) {
		Map<String, Object> settingInfo = DBConn.getSqlSession().selectOne("kr.pe.asuraho.selectTorSetting");
		String job_interval_code = nvl(settingInfo.get("job_interval"));
		String interval = crontabCode(job_interval_code);
		
		JobDetail job = JobBuilder.newJob(SchedulerJob.class).withIdentity("HibogoJob").withDescription("스따또~").build();
		
		Trigger trigger = TriggerBuilder.
							newTrigger().
							withIdentity("HibogoTrigger").
							withSchedule(CronScheduleBuilder.cronSchedule(interval)).
							build();
		try {
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.scheduleJob(job, trigger);
			scheduler.start();
		} catch (SchedulerException e) {
		}
		
/*
		Map<String, Object> settingInfo = null;
		try {
			SqlSession sqlSession = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("resource/mybatis-config.xml")).openSession();
			settingInfo = (Map<String, Object>) sqlSession.selectOne("kr.pe.asuraho.selectTorSetting");
			if(settingInfo == null || "".equals(nvl(settingInfo.get("job_interval")))){
				return;
			}
		} catch (IOException e1) {
e1.printStackTrace();
		}
		
		JobDetail job = JobBuilder.newJob(SchedulerJob.class).withIdentity("HibogoJob").withDescription("너는1번").build();
		
		Trigger trigger = TriggerBuilder.
							newTrigger().
							withIdentity("HibogoTrigger").
							withSchedule(CronScheduleBuilder.cronSchedule(nvl(settingInfo.get("job_interval")))).
							build();
		try {
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.scheduleJob(job, trigger);
			scheduler.start();
			
			Enumeration en = context.getServletContext().getAttributeNames();
			while(en.hasMoreElements()){
				System.out.println(en.nextElement());
			}
			
		} catch (SchedulerException e) {
e.printStackTrace();
		}
		
		
		job = JobBuilder.newJob(SchedulerJob.class).withIdentity("HibogoJob").withDescription("너는2번").build();

		trigger = TriggerBuilder.
							newTrigger().
							withIdentity("HibogoTrigger").
							withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ?")).
							build();
		try {
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.deleteJob(job.getKey());
			
			scheduler.scheduleJob(job, trigger);
			scheduler.start();
			
			Enumeration en = context.getServletContext().getAttributeNames();
			while(en.hasMoreElements()){
				System.out.println(en.nextElement());
			}
			
		} catch (SchedulerException e) {
e.printStackTrace();
		}
*/
	}
}